package com.samapp.map;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.samapp.MyApplication;
import com.samapp.R;
import com.samapp.base.BaseActivity;
import com.samapp.entity.ClientInfo;
import com.samapp.entity.ClientOrder;
import com.samapp.utils.json.GsonUtil;

public class ViewClientActivity extends BaseActivity implements OnClickListener {

	private Button btnBack = null;
	private EditText etNumber = null;
	private EditText etCname = null;
	private EditText etEname = null;
	private EditText etType = null;
	private EditText etCcontacts = null;
	private EditText etEcontacts = null;
	private EditText etFax = null;
	private EditText etEmail = null;
	private EditText etCaddress = null;
	private EditText etEaddress = null;
	private EditText etRemark = null;

	private PullToRefreshListView mPullToRefreshListView = null;
	private ClientIndentAdapter clientIndentAdapter = null;

	/**
	 * 订单数组
	 */
	private List<ClientOrder> mIndents = new ArrayList<ClientOrder>();

	private MyApplication myApplication = MyApplication.getApplicationInstance();

	private final String[] text = { " " };

	/** 创建订单 */
	public static final int MSG_CREATE_CLIENT = 0X194;
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == MSG_CREATE_CLIENT) {
				Toast.makeText(ViewClientActivity.this, text[0], Toast.LENGTH_SHORT).show();
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view_client);
		init();
	}

	private void init() {
		initView();
		initListener();
		initData();
	}

	private void initView() {
		btnBack = (Button) findViewById(R.id.btn_back);
		etNumber = (EditText) findViewById(R.id.number);
		etCname = (EditText) findViewById(R.id.cname);
		etEname = (EditText) findViewById(R.id.ename);
		etType = (EditText) findViewById(R.id.type);
		etCcontacts = (EditText) findViewById(R.id.ccontacts);
		etEcontacts = (EditText) findViewById(R.id.econtacts);
		etFax = (EditText) findViewById(R.id.fax);
		etEmail = (EditText) findViewById(R.id.email);
		etCaddress = (EditText) findViewById(R.id.caddress);
		etEaddress = (EditText) findViewById(R.id.eaddress);
		etRemark = (EditText) findViewById(R.id.remark);
		mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pulltorefresh_client_indent);
		mPullToRefreshListView.setMode(PullToRefreshListView.Mode.BOTH);
		clientIndentAdapter = new ClientIndentAdapter(ViewClientActivity.this, mIndents);
		mPullToRefreshListView.setAdapter(clientIndentAdapter);
	}

	private void initListener() {
		btnBack.setOnClickListener(this);
	}

	private void initData() {
		String curClientFid = myApplication.curClientFid;
		if (myApplication.clientsMap.containsKey(curClientFid)) {
			String url = myApplication.url + "getClientInfo.htm";
			RequestParams params = new RequestParams();
			params.addQueryStringParameter("accessToken", "5C9.yx0S");
			params.addQueryStringParameter("FId", curClientFid);
			new HttpUtils().send(HttpMethod.POST, url, params, new RequestCallBack<String>() {

				@Override
				public void onSuccess(ResponseInfo<String> responseInfo) {
					Log.i("Http", responseInfo.result);
					try {
						ClientInfo client = GsonUtil.getGson().fromJson(responseInfo.result, ClientInfo.class);
						if (client != null) {
							etNumber.setText(client.getNumber());
							etCname.setText(client.getCname());
							etEname.setText(client.getEname());
							etType.setText(client.getType());
							etCcontacts.setText(client.getCcontacts());
							etEcontacts.setText(client.getEcontacts());
							etFax.setText(client.getFax());
							etEmail.setText(client.getEmail());
							etCaddress.setText(client.getAddresscn());
							etEaddress.setText(client.getAddressen());
							etRemark.setText(client.getRemark());

							if (client.getList() != null && client.getList().size() > 0) {
								for (ClientOrder clientOrder : client.getList()) {
									mIndents.add(clientOrder);
								}

								clientIndentAdapter.notifyDataSetChanged();
								mPullToRefreshListView.onRefreshComplete();
							}
						}

						dismissLoadingDialog();
					} catch (Exception e) {
						e.printStackTrace();
						dismissLoadingDialog();
					}
				}

				@Override
				public void onFailure(HttpException e, String arg1) {
					e.printStackTrace();
					dismissLoadingDialog();
				}
			});

			/*
			 * Client client = myApplication.clientsMap.get(curClientFid);
			 * etNumber.setText(client.getNumber());
			 * etCname.setText(client.getCname());
			 * etEname.setText(client.getEname());
			 * etType.setText(client.getType());
			 * etCcontacts.setText(client.getCcontacts());
			 * etEcontacts.setText(client.getEcontacts());
			 * etFax.setText(client.getFax());
			 * etEmail.setText(client.getEmail());
			 * etCaddress.setText(client.getAddresscn());
			 * etEaddress.setText(client.getAddressen());
			 * etRemark.setText(client.getRemark());
			 */
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			skip(ClientDistributionActivity.class);
			finish();
			break;
		}
	}

	/** 跳转页面 */
	private void skip(Class<?> cls) {
		Intent intent = new Intent(this, cls);
		startActivity(intent);
	}

	/** 修改返回键 */
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		btnBack.performClick();
	}

}
